#include "comm.h"

int main()
{

    key_t k = getkey();
    // int n = shmget(key_t key,size_t size, int shmflg);
    // 获取共享内存
    printf("key: 0X%x\n", k);
    int shmid = getShm(k);
    printf("shmid:%d\n", shmid);
    sleep(5);

    // 把共享内存通过页表和进程进行映射
    char *start = (char *)attchShm(shmid);
    printf("attach success, address start:%p\n", start);

    const char *message = "hello server, 我是另外一个进程，正在和你通信";
    pid_t id = getpid();
    int cnt = 1;
    while (1)
    {
        sleep(1);
        // snprintf()往特定的区域做格式化
        snprintf(start, MAX_SIZE, "%s[pid:%d][消息编号:%d]", message, id, cnt);
        printf("消息发送完成,这是第%d条\n", cnt++);

    }

    sleep(5);

    // 删除共享内存
    datechShm(start);
    sleep(5);

    return 0;
}
